package com.yjd.comm.util;

import java.util.Map;
import java.util.Properties;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.mail.model.MailModel;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.vo.Constant.SysParamKeyEnum;

public class MailUtil {

	private static MailUtil util;
	
	private Map<String,String> config;
	
	public Map<String, String> getConfig() {
		return config;
	}

	public void setConfig(Map<String, String> config) {
		this.config = config;
	}

	@SuppressWarnings("unchecked")
	public static synchronized MailUtil getUtil() throws Exception {
		if (util != null)
			return util;
		else{
			SysParamModel param=SysParamUtil.getSysParamModel(SysParamKeyEnum.MAIL_CONFIG);
			Map<String,String> map=(Map<String, String>) JSON.parse(param.getParam_value());
			util=new MailUtil();
			util.setConfig(map);
			return util;
		}
			
	}

	public static synchronized MailUtil getUtil(Map<String,String> map) {
		if (util != null) {
			return util;
		} else {
			util=new MailUtil();
			util.setConfig(map);
			return util;
		}
	}

	private MailUtil() {
	}

	private JavaMailSender mailSender;

	private SimpleMailMessage message;

	public JavaMailSender getMailSender() {
		return resetMailSender(this.config);
	}

	public JavaMailSender resetMailSender(Map<String,String> map) {
		if (mailSender == null) {
			mailSender=new JavaMailSenderImpl();
			((JavaMailSenderImpl)mailSender).setHost(map.get("host"));
			((JavaMailSenderImpl)mailSender).setPassword(map.get("password"));
			((JavaMailSenderImpl)mailSender).setUsername(map.get("username"));
			((JavaMailSenderImpl)mailSender).setPort(Integer.parseInt(map.get("port")));
			Properties p=new Properties();
			p.putAll(map);
			((JavaMailSenderImpl)mailSender).setJavaMailProperties(p);
		} else {
			((JavaMailSenderImpl)mailSender).setHost(map.get("host"));
			((JavaMailSenderImpl)mailSender).setPassword(map.get("password"));
			((JavaMailSenderImpl)mailSender).setUsername(map.get("username"));
			((JavaMailSenderImpl)mailSender).setPort(Integer.parseInt(map.get("port")));
			Properties p=new Properties();
			p.putAll(map);
			((JavaMailSenderImpl)mailSender).setJavaMailProperties(p);
		}
		return mailSender;
	}
	
	public SimpleMailMessage resetMessage(Map<String,String> map) {
		if (message == null) {
			message=new SimpleMailMessage();
			((SimpleMailMessage)message).setSubject(map.get("subject"));
			((SimpleMailMessage)message).setText(map.get("text"));
			((SimpleMailMessage)message).setFrom(map.get("from"));
		} else {
			((SimpleMailMessage)message).setSubject(map.get("subject"));
			((SimpleMailMessage)message).setText(map.get("text"));
			((SimpleMailMessage)message).setFrom(map.get("from"));
		}
		return message;
	}

	public SimpleMailMessage getMessage() {
			return resetMessage(this.config);
	}

	public void sendMail(final MailModel mailModel) throws Exception {
		MimeMessage mimeMessage = getMailSender().createMimeMessage();
		MimeMessageHelper mh = new MimeMessageHelper(mimeMessage,
				MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, "utf-8");
		// 设置抄送
		if (StringUtil.isNotEmpty(mailModel.getCcEmail())) {
			mh.setCc(mailModel.getCcEmail().split(","));
		}
		// 设置暗送
		if (StringUtil.isNotEmpty(mailModel.getBccEmail())) {
			mh.setBcc(mailModel.getBccEmail().split(","));
		}
		// 设置邮件内容
		if (StringUtil.isNotEmpty(mailModel.getMailContent())) {
			mh.setText(mailModel.getMailContent(), true);
		} else {
			mh.setText(getMessage().getText(), true);
		}
		// 设置邮件主题
		if (StringUtil.isNotEmpty(mailModel.getSubject())) {
			mh.setSubject(mailModel.getSubject());
		} else {
			mh.setSubject(getMessage().getSubject());
		}
		// 设置邮件发送人
		if (StringUtil.isNotEmpty(mailModel.getFromEmail())) {
			mh.setFrom(mailModel.getFromEmail(), getMessage().getFrom());
		} else {
			mh.setFrom(((JavaMailSenderImpl) getMailSender()).getUsername(),
					getMessage().getFrom());
		}
		// 设置邮件接收人
		mh.setTo(new InternetAddress(mailModel.getToEmail()));
		mimeMessage.saveChanges();
		// 发送邮件
		getMailSender().send(mimeMessage);
	}
}
